#include "main.h"

// Initialize joystick
Joystick joystick(PC_2, PC_3);

// Joystick initialization
void StickInit()
{
    joystick.init(); 
}

// Get current joystick values (angle and magnitude)
StickValues stick_value_get()
{
    Polar polar = joystick.get_polar();  

    StickValues result;
    result.angle = (int)polar.angle;        // Angle in degrees
    result.length = (int)(polar.mag * 100);  // Magnitude scaled to 0–100
    return result; 
}

// Determine joystick direction
int get_direction()
{
    int angle, mag;
    StickValues stickValues = stick_value_get();
    angle = stickValues.angle;
    mag = stickValues.length;

    if (mag > 30) { // Only consider if joystick is pushed far enough
        if (angle >= 337.5 || angle < 22.5)  return 0; // Right
        if (angle >= 22.5  && angle < 67.5)  return 1; // Up-Right
        if (angle >= 67.5  && angle < 112.5) return 2; // Up
        if (angle >= 112.5 && angle < 157.5) return 3; // Up-Left
        if (angle >= 157.5 && angle < 202.5) return 4; // Left
        if (angle >= 202.5 && angle < 247.5) return 5; // Down-Left
        if (angle >= 247.5 && angle < 292.5) return 6; // Down
        if (angle >= 292.5 && angle < 337.5) return 7; // Down-Right
    }
    return 8; // Center (no movement)
}
